/**
 * Created by SCR on 2017/8/9.
 */

var path = require('path');
var formidable = require('formidable');

var setting = require('../setting');
var VisualCtrl = require('../control/Integrate/VisualizationCtrl');
var VisualModel = require('../model/Integrate/visualization');
// var FileOperate = require('../utils/fileOpera');
var rimraf = require('rimraf');

module.exports = function (app) {
    app.route('/visualization/:ac')
        .get(function (req, res, next) {
            var ac = req.params.ac;

            if(ac == 'query'){
                VisualCtrl.getPackageList(function (err, visualList) {
                    if(err){
                        return res.render('visualization/visualLib',{
                            error: JSON.stringify(err)
                        });
                    }
                    else{
                        return res.render('visualization/visualLib',{
                            error: '',
                            visualList: JSON.stringify(visualList)
                        });
                    }
                });
            }

            else if(ac == 'detail'){
                var _id = req.query._id;
                VisualCtrl.getPackageDetail(_id,function (err, visualService) {
                    if(err){
                        return res.render('visualization/visualDetail',{
                            error: JSON.stringify({error: err})
                        });
                    }
                    return res.render('visualization/visualDetail',{
                        error: '',
                        visualService: visualService
                    });
                })
            }

            // TODO
            else if(ac == 'new'){
                var form = new formidable.IncomingForm();
                form.encoding = 'utf-8';
                form.uploadDir = path.join(setting.modelpath,'/upload/');
                form.keepExtensions = true;
                form.maxFieldsSize = 100 * 1024 * 1024;

                form.parse(req, function (err, fields, files) {
                    if(err) {
                        return res.end(JSON.stringify({error: err}));
                    }
                    VisualCtrl.uploadPackage(fields,files,function (err, _id) {
                        if(err){
                            return res.end(JSON.stringify({error: err}));
                        }
                        return res.end(JSON.stringify({error: null, _id: _id}));
                    });
                });
            }

            else {
                next();
            }
        });

    app.route('/visualization/delete')
        .delete(function (req, res, next) {
            var _id = req.body._id;
            VisualModel.delete(_id,function (err, rst) {
                if(err){
                    return res.end(JSON.stringify({error:err}));
                }
                else{
                    var fpath = path.join(__dirname, '../visualization_service',_id);
                    rimraf(fpath,function (err) {
                        if(err){
                            return res.end(JSON.stringify({error:err}));
                        }
                        else{
                            return res.end(JSON.stringify({error:null,result:rst}));
                        }
                    });
                }
            });
        });
};